<?php

/**
 * 设备分析
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';

class Admin_DeviceController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_Device();
        $this->_productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }
        
        $vars['dateType']  = trim($this->_request->get('dateType'))!='' ? trim($this->_request->get('dateType')) : 2;
        $vars['doType'] = 1;//trim($this->_request->get('doType'))!='' ? trim($this->_request->get('doType')) : 1;
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        
        if ($vars['dateType']==2){
            $vars['startTime'] = date('Y-m-d',strtotime("-7 day"));
            $vars['endTime']   = date('Y-m-d',strtotime("-1 day"));
        } elseif ($vars['dateType']==3){
            $vars['startTime'] = date('Y-m-d',strtotime("-30 day"));
            $vars['endTime']   = date('Y-m-d',strtotime("-1 day"));
        } elseif ($vars['dateType']==4){
            $vars['startTime'] = date('Y-m-d',strtotime("-3 months"));
            $vars['endTime']   = date('Y-m-d',strtotime("-1 day"));
        } elseif ($vars['dateType']==5){
            $vars['startTime'] = '';
            $vars['endTime']   = '';
        }
        
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);
        
        $wheres[] = "`productCode` = '{$this->_productCode}'";
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
        }
        //新增用户
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT sum(installNum) as installNum,deviceName 
                FROM {$this->_model->getTableName()} " . $where . 
                ' GROUP BY deviceName 
                  ORDER BY installNum DESC 
                ';
        $resTmp = $this->_model->getAdapter()->fetchAll($sql);
        $resTmpCou = 0;
        foreach ($resTmp as $item) {
            $resTmpCou += $item['installNum'];
        }
        $couArr = array();
        foreach ($resTmp as $key => $item) {
            if($key < 20) {
                $couArr[$key]['installNum'] = $item['installNum'];
                $couArr[$key]['deviceName'] = $item['deviceName'];
                $couArr[$key]['per'] = ($resTmpCou!=0) ? round((($item['installNum']/$resTmpCou)*100), 2) : '';
                $couArr[$key]['width'] = ($resTmp[0]['installNum']!=0) ? round($item['installNum']/$resTmp[0]['installNum'], 4) : '';
            }
        }
        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();
        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
            
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $listArr = array();
        foreach ($dbPaginator->getRows() as $key => $item) {
            $listArr[$key]['installNum'] = $item['installNum'];
            $listArr[$key]['deviceName'] = $item['deviceName'];
            $listArr[$key]['per'] = ($resTmpCou!=0) ? round((($item['installNum']/$resTmpCou)*100), 2) : '';
        }
        $this->view->rows = $listArr;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->couArr = $couArr;
        $this->view->vars = $vars;
        //累积用户
        $sqlAllUser = "SELECT sum(installNum) as installNum,deviceName 
                FROM {$this->_model->getTableName()} 
                WHERE `productCode` = '{$this->_productCode}'
                GROUP BY deviceName 
                ORDER BY installNum DESC 
                ";
        $allUserArr = $this->_model->getAdapter()->fetchAll($sqlAllUser);
        $allInstallNum = 0.000001;
        foreach ($allUserArr as $key => $item) {
            $allInstallNum += $item['installNum'];
        }
        $allArr = array();
        foreach ($allUserArr as $key => $item) {
            if($key < 20) {
                $allArr[$key]['installNum'] = $item['installNum'];
                $allArr[$key]['deviceName'] = $item['deviceName'];
                $allArr[$key]['per'] = ($allInstallNum!=0) ? round((($item['installNum']/$allInstallNum)*100), 2) : '';
                $allArr[$key]['width'] = round($item['installNum']/$allUserArr[0]['installNum'], 4);
            }
        }
        $this->view->allArr = $allArr;
    }
    
    
    
    
   
}